﻿namespace ExamV5.StudyInfo
{
    using ExamV5;
    using System;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    public class LessionManagType : Page
    {
        protected Button butDefault;
        protected Button butDel;
        protected DropDownList DDLUPFolder;
        protected HtmlForm form1;
        protected HtmlHead Head1;
        private HttpCookie httpCookie_0 = null;
        private HttpCookie httpCookie_1 = null;
        private HttpCookie httpCookie_2 = null;
        private HttpCookie httpCookie_3 = null;
        private HttpCookie httpCookie_4 = null;
        protected Label labID;
        private PublicFun publicFun_0 = new PublicFun();
        protected ScriptManager SM;
        protected string strBrowDeptID = "";
        protected string strBrowDeptName = "";
        protected string strBrowUserCode = "";
        protected string strBrowUserName = "";
        protected string strEditUserCode = "";
        protected string strEditUserName = "";
        protected string strID = "0";
        //protected TreeView TreeDept;
        protected TextBox txtFolderName;
        protected TextBox txtSortInfo;
        protected UpdatePanel UPPart;

        protected Label lblTreeType;

        protected void butDefault_Click(object sender, EventArgs e)
        {
            if ((this.labID.Text.Trim() == "0") || (this.labID.Text != this.DDLUPFolder.SelectedItem.Value))
            {
                string str2 = "";
                string str3 = "";
                str2 = base.Request.Form["txtBrowDeptID"].ToString();
                str3 = base.Request.Form["txtBrowDeptName"].ToString();
                if (str2.Trim() != "")
                {
                    str2 = "," + str2 + ",";
                    str3 = "," + str3 + ",";
                }
                else
                {
                    str2 = ",所有部门,";
                    str3 = ",所有部门,";
                }
                string str6 = "";
                string str7 = "";
                str6 = base.Request.Form["txtBrowUserCode"].ToString();
                str7 = base.Request.Form["txtBrowUserName"].ToString();
                if (!(str6.Trim() != ""))
                {
                    str6 = ",所有人员,";
                    str7 = ",所有人员,";
                }
                else
                {
                    str6 = "," + str6 + ",";
                    str7 = "," + str7 + ",";
                }
                string str4 = "";
                string str5 = "";
                str4 = base.Request.Form["txtEditUserCode"].ToString();
                str5 = base.Request.Form["txtEditUserName"].ToString();
                if (str4.Trim() != "")
                {
                    str4 = "," + str4 + ",";
                    str5 = "," + str5 + ",";
                }
                else
                {
                    str4 = "," + this.httpCookie_0.Value + ",";
                    str5 = "," + this.httpCookie_1.Value + ",";
                }
                string strSql = "";
                if (!(this.labID.Text == "0"))
                {
                    strSql = "update TypeList set BrowDeptID='" + str2 + "',BrowDeptName='" + str3 + "',Flag='LessionType',InputUser='" + this.httpCookie_0.Value + "',EditUserCode='" + str4 + "',EditUserName='" + str5 + "',BrowUserCode='" + str6 + "',BrowUserName='" + str7 + "',SortInfo='" + this.txtSortInfo.Text + "',TypeName='" + this.publicFun_0.CheckString(this.txtFolderName.Text.Trim()) + "',UPID='" + this.DDLUPFolder.SelectedItem.Value + "' where ID=" + this.labID.Text + ";insert into SysLogInfo(LogDisp,EditUser,EditUserCode) values('编辑[" + this.publicFun_0.CheckString(this.txtFolderName.Text) + "]课件类别信息！','" + this.httpCookie_1.Value + "','" + this.httpCookie_0.Value + "')";
                }
                else
                {
                    strSql = "Insert into TypeList(BrowDeptID,BrowDeptName,SortInfo,TypeName,UPID,EditUserCode,EditUserName,BrowUserCode,BrowUserName,InputUser,Flag) values('" + str2 + "','" + str3 + "','" + this.txtSortInfo.Text + "','" + this.publicFun_0.CheckString(this.txtFolderName.Text.Trim()) + "','" + this.DDLUPFolder.SelectedItem.Value + "','" + str4 + "','" + str5 + "','" + str6 + "','" + str7 + "','" + this.httpCookie_0.Value + "','LessionType');insert into SysLogInfo(LogDisp,EditUser,EditUserCode) values('新 增[" + this.publicFun_0.CheckString(this.txtFolderName.Text) + "]课件类别信息！','" + this.httpCookie_1.Value + "','" + this.httpCookie_0.Value + "')";
                }
                this.publicFun_0.ExecuteSql(strSql);
                this.strID = "0";
                this.butDefault.Text = "新 增";
                ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BCheck", "alert('编辑课件类别信息操作成功！');setTimeout(function(){window.location='LessionManagType.aspx'},500)", true);
            }
            else
            {
                ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BCheck", "alert('上级类别名称不可以为当前类别名称！')", true);
            }
        }

        protected void butDel_Click(object sender, EventArgs e)
        {
            if (!(this.labID.Text.Trim() != "0"))
            {
                ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BCheck", "alert('请选择需要删除的课件类别信息！')", true);
            }
            else if (!((this.publicFun_0.GetValues("select ID from LessionInfo where LessionTypeID=" + this.labID.Text.Trim(), "ID") == "") ? !(this.publicFun_0.GetValues("select ID from TypeList where UPID=" + this.labID.Text.Trim(), "ID") == "") : true))
            {
                this.publicFun_0.ExecuteSql("Delete from TypeList where ID=" + this.labID.Text.Trim() + ";insert into SysLogInfo(LogDisp,EditUser,EditUserCode) values('删除[" + this.publicFun_0.CheckString(this.txtFolderName.Text) + "]课件类别信息！','" + this.httpCookie_1.Value + "','" + this.httpCookie_0.Value + "')");
                ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BCheck", "alert('删除课件类别信息操作成功！');setTimeout(function(){window.location='LessionManagType.aspx'},500)", true);
                this.butDefault.Text = "新 增";
                this.strID = "0";
            }
            else
            {
                ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BCheck", "alert('此课件类别档案中还存在课件或子类别信息,无法删除！')", true);
            }
        }

        private void method_0(string string_0, TreeNode treeNode_0, string string_1, string string_2)
        {
            //TreeNode child = new TreeNode {
            //    Value = string_1,
            //    Text = string_0,
            //    ToolTip = string_2
            //};
            //if (!(!(string_2 == "0") ? !(string_0 == "课件类别信息管理") : false))
            //{
            //    this.TreeDept.Nodes.Add(child);
            //}
            //else
            //{
            //    treeNode_0.ChildNodes.Add(child);
            //    treeNode_0.Expanded = true;
            //}
            //string cmdText = "select * from TypeList where flag='LessionType' and UPID='" + string_1 + "' and  EditUserCode like '%," + this.httpCookie_0.Value + ",%'  order by SortInfo,ID";
            //if (this.ViewState["ManagPower"].ToString() == "Y")
            //{
            //    cmdText = "select * from TypeList where flag='LessionType' and UPID='" + string_1 + "'  order by SortInfo,ID";
            //}
            //SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
            //SqlCommand command = new SqlCommand(cmdText, connection);
            //connection.Open();
            //SqlDataReader reader = command.ExecuteReader();
            //while (reader.Read())
            //{
            //    this.method_0(reader["TypeName"].ToString(), child, reader["ID"].ToString(), reader["UPID"].ToString());
            //}
            //reader.Close();
            //connection.Close();
            //connection.Dispose();
        }

        private void method_0(string string_0, string string_1, string string_2)
        {
            if (string_0 == "课件类别信息管理")
            {
                this.lblTreeType.Text += "<ul id=\"red\" class=\"treeview-red\">";
            }
            else
            {
                this.lblTreeType.Text += "<ul>";
            }
            string cmdText = "select * from TypeList where flag='LessionType' and UPID='" + string_1 + "' and  EditUserCode like '%," + this.httpCookie_0.Value + ",%'  order by SortInfo,ID";
            if (this.ViewState["ManagPower"].ToString() == "Y")
            {
                cmdText = "select * from TypeList where flag='LessionType' and UPID='" + string_1 + "'  order by SortInfo,ID";
            }
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
            SqlCommand command = new SqlCommand(cmdText, connection);
            connection.Open();
            SqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                var name = reader["TypeName"].ToString();
                var id = reader["ID"].ToString();
                var upid = reader["UPID"].ToString();
                this.lblTreeType.Text += $"<li><span><a style=\"cursor:pointer\" onclick=\"TreeClicked('{name}','{id}','{upid}')\">{name}</a></span>";
                //this.lblTreeType.Text += $"<li><span><a style=\"cursor:pointer\">{name}</a></span>";
                this.method_0(name, id, upid);
                this.lblTreeType.Text += "</li>";
            }
            reader.Close();
            connection.Close();
            connection.Dispose();

            this.lblTreeType.Text += "</ul>";
            this.lblTreeType.Text = this.lblTreeType.Text.Replace("<ul></ul>", "");
            this.lblTreeType.Text = this.lblTreeType.Text.Replace("<ul id=\"red\" class=\"treeview-red\"></ul>", "");
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.httpCookie_0 = new HttpCookie("ExamUserCode");
            this.httpCookie_0 = base.Request.Cookies["ExamUserCode"];
            this.httpCookie_1 = new HttpCookie("ExamUserName");
            this.httpCookie_1 = base.Request.Cookies["ExamUserName"];
            this.httpCookie_3 = new HttpCookie("ExamUserLevel");
            this.httpCookie_3 = base.Request.Cookies["ExamUserLevel"];
            this.httpCookie_4 = new HttpCookie("ExamDeptID");
            this.httpCookie_4 = base.Request.Cookies["ExamDeptID"];
            this.httpCookie_2 = new HttpCookie("ExamDeptName");
            this.httpCookie_2 = base.Request.Cookies["ExamDeptName"];
            if ((this.httpCookie_0 != null) ? !(this.httpCookie_0.Value == "") : false)
            {
                base.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                base.Response.Cache.SetNoStore();
                this.httpCookie_0.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_0.Value));
                this.httpCookie_1.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_1.Value));
                this.httpCookie_3.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_3.Value));
                this.httpCookie_4.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_4.Value));
                this.httpCookie_2.Value = this.publicFun_0.CheckString(base.Server.UrlDecode(this.httpCookie_2.Value));
                if (base.IsPostBack)
                {
                    this.strBrowUserCode = base.Request.Form["txtBrowUserCode"].ToString();
                    this.strBrowUserName = base.Request.Form["txtBrowUserName"].ToString();
                    this.strEditUserCode = base.Request.Form["txtEditUserCode"].ToString();
                    this.strEditUserName = base.Request.Form["txtEditUserName"].ToString();
                    this.strBrowDeptID = base.Request.Form["txtBrowDeptID"].ToString();
                    this.strBrowDeptName = base.Request.Form["txtBrowDeptName"].ToString();
                }
                else if (this.publicFun_0.ReturnPaperData() != "Yes")
                {
                    ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BCheck", "alert('此版本暂未授权,请拔打(0)13870606104手机号进行授权！')", true);
                }
                else
                {
                    ListItem item;
                    this.ViewState["ManagPower"] = this.publicFun_0.GetValues("select ManagLession from UserPower where UserCode='" + this.httpCookie_0.Value + "'", "ManagLession");
                    this.ViewState["AddPower"] = this.publicFun_0.GetValues("select AddLession from UserPower where UserCode='" + this.httpCookie_0.Value + "'", "AddLession");
                    this.Page.Title = ConfigurationManager.ConnectionStrings["ProTitle"].ConnectionString;
                    this.DDLUPFolder.Items.Clear();
                    if (this.ViewState["ManagPower"].ToString() != "Y")
                    {
                        if (!(this.ViewState["AddPower"].ToString() == "Y"))
                        {
                            this.DDLUPFolder.Items.Clear();
                            ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "BCheckA", "alert('管理或添加电子课件权限未开放！')", true);
                            this.butDefault.Visible = false;
                            this.butDel.Visible = false;
                        }
                        else
                        {
                            this.publicFun_0.GetType(this.DDLUPFolder, "LessionType", this.httpCookie_0.Value, "N", this.httpCookie_4.Value);
                            this.DDLUPFolder.SelectedIndex = -1;
                            item = new ListItem("顶级类别", "0");
                            this.DDLUPFolder.Items.Insert(0, item);
                        }
                    }
                    else
                    {
                        this.publicFun_0.GetType(this.DDLUPFolder, "LessionType", this.httpCookie_0.Value, "Y", this.httpCookie_4.Value);
                        this.DDLUPFolder.SelectedIndex = -1;
                        item = new ListItem("顶级类别", "0");
                        this.DDLUPFolder.Items.Insert(0, item);
                    }
                    this.lblTreeType.Text = string.Empty;
                    //this.TreeDept.Nodes.Clear();
                    //TreeNode node = new TreeNode();
                    if (this.ViewState["ManagPower"].ToString() == "Y")
                    {
                        //this.method_0("课件类别信息管理", node, "0", "0");
                        this.method_0("课件类别信息管理", "0", "0");
                    }
                    else
                    {
                        string str = "";
                        SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
                        connection.Open();
                        SqlCommand command = new SqlCommand("select ID,TypeName from TypeList where flag='LessionType' and  EditUserCode like '%," + this.httpCookie_0.Value + ",%' and UPID=0 order by UPID,SortInfo ", connection);
                        SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
                        while (reader.Read())
                        {
                            str = reader["TypeName"].ToString();
                            this.strID = reader["ID"].ToString();
                            if (str != "")
                            {
                                //this.method_0(str, node, this.strID, "0");
                                this.method_0(str, this.strID, "0");
                            }
                        }
                        reader.Close();
                        command.Dispose();
                        connection.Close();
                        connection.Dispose();
                    }
                    this.butDel.Attributes.Add("onclick", "javascript:{if(confirm('确定要删除吗？')==false) return false;}");
                    if (this.httpCookie_3.Value == "C")
                    {
                        this.strBrowUserCode = "所有人员";
                        this.strBrowUserName = "所有人员";
                        this.strBrowDeptID = "所有部门";
                        this.strBrowDeptName = "所有部门";
                    }
                    else
                    {
                        this.strBrowUserCode = this.httpCookie_0.Value;
                        this.strBrowUserName = this.httpCookie_1.Value;
                        this.strBrowDeptID = this.httpCookie_4.Value;
                        this.strBrowDeptName = this.httpCookie_2.Value;
                    }
                    this.strEditUserCode = this.httpCookie_0.Value;
                    this.strEditUserName = this.httpCookie_1.Value;
                    this.txtSortInfo.Text = "1";
                    this.butDefault.Text = "新 增";
                }
            }
            else
            {
                base.Response.Write("<script language=javascript>top.location.href='../Index.htm';self.close();</script>");
            }
        }
        protected void btnGet_Click(object sender, EventArgs e)
        {
            var name = base.Request.Form["txtTypeName"].ToString();
            var id = base.Request.Form["txtTypeId"].ToString();
            var upid = base.Request.Form["txtTypeUPId"].ToString();
            this.ShowData(name, id, upid);
            ScriptManager.RegisterStartupScript(this.UPPart, this.Page.GetType(), "Render", "LayaUIFormRender();", true);
        }

        public void ShowData(string name, string id, string upid)
        {
            if (name == "课件类别信息管理")
            {
                this.txtFolderName.Text = "";
                this.DDLUPFolder.SelectedIndex = -1;
                this.labID.Text = "0";
                if (!(this.httpCookie_3.Value == "C"))
                {
                    this.strBrowUserCode = this.httpCookie_0.Value;
                    this.strBrowUserName = this.httpCookie_1.Value;
                    this.strBrowDeptID = this.httpCookie_4.Value;
                    this.strBrowDeptName = this.httpCookie_2.Value;
                }
                else
                {
                    this.strBrowUserCode = "所有人员";
                    this.strBrowUserName = "所有人员";
                    this.strBrowDeptID = "所有部门";
                    this.strBrowDeptName = "所有部门";
                }
                this.strEditUserCode = this.httpCookie_0.Value;
                this.strEditUserName = this.httpCookie_1.Value;
                this.txtSortInfo.Text = "1";
                this.strID = this.labID.Text;
                this.butDefault.Text = "新 增";
            }
            else
            {
                this.txtFolderName.Text = name;
                this.DDLUPFolder.SelectedIndex = -1;
                this.DDLUPFolder.Items.FindByValue(upid).Selected = true;
                this.labID.Text = id;
                this.strID = this.labID.Text;
                SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
                connection.Open();
                SqlCommand command = new SqlCommand("select * from TypeList where ID='" + id.Trim() + "'", connection);
                SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
                if (reader.Read())
                {
                    this.strBrowUserCode = reader["BrowUserCode"].ToString().Substring(1, reader["BrowUserCode"].ToString().Length - 2);
                    this.strBrowUserName = reader["BrowUserName"].ToString().Substring(1, reader["BrowUserName"].ToString().Length - 2);
                    this.strEditUserCode = reader["EditUserCode"].ToString().Substring(1, reader["EditUserCode"].ToString().Length - 2);
                    this.strEditUserName = reader["EditUserName"].ToString().Substring(1, reader["EditUserName"].ToString().Length - 2);
                    this.txtSortInfo.Text = reader["SortInfo"].ToString();
                    this.strID = id;
                    this.strBrowDeptID = reader["BrowDeptID"].ToString().Substring(1, reader["BrowDeptID"].ToString().Length - 2);
                    this.strBrowDeptName = reader["BrowDeptName"].ToString().Substring(1, reader["BrowDeptName"].ToString().Length - 2);
                }
                reader.Close();
                command.Dispose();
                connection.Close();
                connection.Dispose();
                this.butDefault.Text = "修 改";
            }
        }
        //protected void TreeDept_SelectedNodeChanged(object sender, EventArgs e)
        //{
        //    TreeNode selectedNode = this.TreeDept.SelectedNode;
        //    if (selectedNode.Text != "课件类别信息管理")
        //    {
        //        this.txtFolderName.Text = selectedNode.Text;
        //        this.DDLUPFolder.SelectedIndex = -1;
        //        this.DDLUPFolder.Items.FindByValue(selectedNode.ToolTip).Selected = true;
        //        this.labID.Text = selectedNode.Value;
        //        this.strID = this.labID.Text;
        //        SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
        //        connection.Open();
        //        SqlCommand command = new SqlCommand("select * from TypeList where ID='" + selectedNode.Value.Trim() + "'", connection);
        //        SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
        //        if (reader.Read())
        //        {
        //            this.strBrowUserCode = reader["BrowUserCode"].ToString().Substring(1, reader["BrowUserCode"].ToString().Length - 2);
        //            this.strBrowUserName = reader["BrowUserName"].ToString().Substring(1, reader["BrowUserName"].ToString().Length - 2);
        //            this.strEditUserCode = reader["EditUserCode"].ToString().Substring(1, reader["EditUserCode"].ToString().Length - 2);
        //            this.strEditUserName = reader["EditUserName"].ToString().Substring(1, reader["EditUserName"].ToString().Length - 2);
        //            this.txtSortInfo.Text = reader["SortInfo"].ToString();
        //            this.strID = selectedNode.Value;
        //            this.strBrowDeptID = reader["BrowDeptID"].ToString().Substring(1, reader["BrowDeptID"].ToString().Length - 2);
        //            this.strBrowDeptName = reader["BrowDeptName"].ToString().Substring(1, reader["BrowDeptName"].ToString().Length - 2);
        //        }
        //        reader.Close();
        //        command.Dispose();
        //        connection.Close();
        //        connection.Dispose();
        //        this.butDefault.Text = "修 改";
        //    }
        //    else
        //    {
        //        this.txtFolderName.Text = "";
        //        this.DDLUPFolder.SelectedIndex = -1;
        //        this.labID.Text = "0";
        //        if (!(this.httpCookie_3.Value == "C"))
        //        {
        //            this.strBrowUserCode = this.httpCookie_0.Value;
        //            this.strBrowUserName = this.httpCookie_1.Value;
        //            this.strBrowDeptID = this.httpCookie_4.Value;
        //            this.strBrowDeptName = this.httpCookie_2.Value;
        //        }
        //        else
        //        {
        //            this.strBrowUserCode = "所有人员";
        //            this.strBrowUserName = "所有人员";
        //            this.strBrowDeptID = "所有部门";
        //            this.strBrowDeptName = "所有部门";
        //        }
        //        this.strEditUserCode = this.httpCookie_0.Value;
        //        this.strEditUserName = this.httpCookie_1.Value;
        //        this.txtSortInfo.Text = "1";
        //        this.strID = this.labID.Text;
        //        this.butDefault.Text = "新 增";
        //    }
        //}
    }
}

